pacman::p_load(readxl, gifski, gapminder,
plotly, gganimate, tidyverse)Hands-on Exercise 3.2
This is the Hands-on Exercise 3.2
Getting Started
- plotly is the R library for plotting interactive statistical graphs.
- gganimate is a ggplot extension for creating animated stastical graphs.
- gifski converts video frames to GIF animations using pngquant’s fancy features for efficient cross-frame palettes and temporal dithering. It produces animated GIFs that use thousands of colors per frame.
- gapminder is an excerpt of the data available at Gapminder.org. We just want to use its country_colors scheme.
Loading Packages
Importing Data
col <- c("Country", "Continent")
globalPop <- read_xls("data/GlobalPopulation.xls",sheet="Data") %>%
mutate_at(col, as.factor) %>%
mutate(Year = as.integer(Year))Animated Data Visualisation: gganimate
- transition_*() defines how the data should be spread out and how it relates to itself across time.
- view_*() defines how the positional scales should change along the animation.
- shadow_*() defines how data from other points in time should be presented in the given point in time.
- enter_()/exit_() defines how new data should appear and how old data should disappear during the course of the animation.
- ease_aes() defines how different aesthetics should be eased during transitions.
Building a static population bubble plot
ggplot(globalPop, aes(x = Old, y = Young, size = Population, colour = Country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = '% Aged', y = '% Young') 
Building the animated bubble plot
ggplot(globalPop, aes(x = Old, y = Young, size = Population, colour = Country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = '% Aged', y = '% Young') +
transition_time(Year) +
ease_aes('linear')
Using other gganimate
ggplot(globalPop, aes(x = Old, y = Young, size = Population, colour = Country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = '% Aged', y = '% Young') +
transition_time(Year) +
view_follow(fixed_x = TRUE, fixed_y = TRUE) +
shadow_trail(0.1) +
enter_grow() +
exit_shrink() +
ease_aes("sine-in-out")
Animated Data Visualisation: plotly
Building an animated bubble plot: ggplotly() method
gg <- ggplot(globalPop,
aes(x = Old,y = Young, size = Population, colour = Country)) +
geom_point(aes(size = Population, frame = Year), alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged', y = '% Young')
ggplotly(gg)gg <- ggplot(globalPop,
aes(x = Old,y = Young, size = Population, colour = Country)) +
geom_point(aes(size = Population, frame = Year), alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged', y = '% Young') +
theme(legend.position = 'none')
ggplotly(gg)Building an animated bubble plot: plot_ly() method
bp <- globalPop %>%
plot_ly(x = ~Old, y = ~Young, size = ~Population, color = ~Continent, sizes = c(2, 100), frame = ~Year, text = ~Country, hoverinfo = "text", type = 'scatter', mode = 'markers') %>%
layout(showlegend = FALSE)
bp